package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.*;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.*;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    //根据名字查找用户
    @Override
    public User findByUsername(String username) {
        //查找用户
        User user = userDao.findByUsername(username);
        if (user != null) {

            Integer userId = user.getId();
            //查找角色
            Set<Role> roles = roleDao.findByUserId(userId);
            user.setRoles(roles);

            if (roles != null && roles.size() > 0) {

                //查找权限
                for (Role role : roles) {
                    Integer roleId = role.getId();
                    Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                    role.setPermissions(permissions);
                }
            }
        }
        return user;
    }

    @Autowired
    private MenuDao menuDao;


    //根据用户名查找user
    @Override
    public List<Menu> findUserByUsername(String username) {

        //
        User user = userDao.findByUsername(username);
        if (user != null) {

            Integer userId = user.getId();
            //查找角色
            Set<Role> roles = roleDao.findByUserId(userId);
            Map<String, Menu> map = new HashMap<>();
            if (roles != null) {

                for (Role role : roles) {
                    Integer roleId = role.getId();
                    List<Menu> menus = menuDao.findByRoleId(roleId);
                    return menus;
                }
            }


        }
        return null;
    }

    //分页
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<User> page = userDao.findPage(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //添加
    public void add(User user, Integer[] roleIds) {
        //添加用户信息
        userDao.add(user);
        //将用户与角色关联起来
        setUserIdAndRoleIds(user.getId(), roleIds);
    }

    //根据id查询用户
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    //根据userId查询roleIds
    public List<Integer> findRoleIdsByUserId(Integer userId) {
        return userDao.findRoleIdsByUserId(userId);
    }

    //修改用户信息
    public void edit(User user, Integer[] roleIds) {
        //1.将页面传来的用户数据在原有的数据上进行修改
        userDao.edit(user);
        //2.删除用户与角色之间的关联
        userDao.deleteUserAssociateRole(user.getId());
        //3.在用户与角色之间建立新的关联
        setUserIdAndRoleIds(user.getId(), roleIds);
    }

    //删除用户
    public void delete(Integer id) {
        //1.删除用户与角色关联
        userDao.deleteUserAssociateRole(id);
        //删除用户
        userDao.delete(id);
    }

    //将用户与角色关联起来
    public void setUserIdAndRoleIds(Integer userId, Integer[] roleIds) {
        for (Integer roleId : roleIds) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("userId", userId);
            map.put("roleId", roleId);
            userDao.setUserIdAndRoleIds(map);
        }
    }


}
